package com.ponhu.ea2.stockcenter.provider.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Created by lenovo on 2018/5/16.
 */
public class PropertiesUtils
{
    /**
     * 日志记录类
     */
    private final static Logger logger = LogManager.getLogger(PropertiesUtils.class);
    private static Properties config = null;
    static
    {
        InputStream in = PropertiesUtils.class.getClassLoader().getResourceAsStream("ph_config.properties");
        config = new Properties();
        try
        {
            config.load(in);
            in.close();
        }
        catch (IOException e)
        {
            logger.error(e);
            System.out.println("");
        }
    }


    /**
     * 读取properties的全部信息
     *
     * @see [类、类#方法、类#成员]
     */
    public static void readAllProperties()
    {
        try
        {
            Enumeration en = config.propertyNames();
            while (en.hasMoreElements())
            {
                String key = (String) en.nextElement();
                String Property = config.getProperty(key);
                System.out.println(key + Property);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            logger.error("ConfigInfoError" + e.toString());
        }
    }


    /**
     * 根据key读取value
     *
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String readValue(String key)
    {
        try
        {
            String value = config.getProperty(key);
            return value;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            logger.error("ConfigInfoError" + e.toString());
            return null;
        }
    }}